package utils
{
	import flash.display.DisplayObject;
	
	import mx.collections.ArrayCollection;
	import mx.controls.CheckBox;
	import mx.controls.ColorPicker;
	import mx.controls.ComboBox;
	import mx.controls.DataGrid;
	import mx.controls.DateField;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.List;
	import mx.controls.NumericStepper;
	import mx.controls.RadioButton;
	import mx.controls.RichTextEditor;
	import mx.controls.Text;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.core.UIComponent;
	
	public class SMComponentUtils
	{
		
		static public function findValueForDisplayObject(field:DisplayObject) : Object
		{
			if (field is TextInput)
			{
				return SMStringUtils.trim(TextInput(field).text);
			}
			else if (field is TextArea)
			{
				
				var jcTextArea:TextArea = field as TextArea;
				
			}
			else if (field is RichTextEditor)
			{
				
				var jcRichTextEditor:RichTextEditor = field as RichTextEditor;
				
			}
			else if (field is DateField) 
			{
				return DateField(field).selectedDate;
			}
			else if (field is ComboBox)
			{
				return ComboBox(field).selectedItem;				
			}
			else if (field is CheckBox) 
			{
				return CheckBox(field).selected;
			}
			else if (field is RadioButton) 
			{
				
				if (RadioButton(field).selected)
				{
					return RadioButton(field).value;
				}
				
				return null;
			}
			else if (field is NumericStepper) 
			{
				return NumericStepper(field).value;
			}
			else if (field is Image) 
			{
				return SMUtils.convertImageToByteArray(Image(field));
			}
			else if (field is DataGrid) 
			{
				return DataGrid(field).dataProvider;
			}
			else if (field is List) 
			{
				
				var jcList:List = field as List;
				
				if (!SMUtils.isNullOrEmpty(jcList.selectedItems))
				{
					return new ArrayCollection(jcList.selectedItems);
				}
				
				return null;
				
			}
			else if (field is ColorPicker) 
			{
				return ColorPicker(field).selectedColor;
			}
			else if (field is Text) 
			{
				
				var jcText:Text = field as Text;
				
				return SMStringUtils.trim(jcText.text);
				
			}
			else if (field is Label) 
			{
				
				var jcLabel:Label = field as Label;
				
				return SMStringUtils.trim(jcLabel.text);
				
			}
			
			return null;
			
		}
		
		static public function selectsFields(fields:Array, values:Array, campo:String = 'codigo') : void 
		{
			
			if (SMUtils.isNullOrEmpty(fields) || SMUtils.isNullOrEmpty(values) || fields.length != values.length) 
			{
				return;
			}
			
			for (var i:int = 0; i < fields.length; i++) 
			{
				selectField(fields[i], values[i], campo);
			}
			
		}
		
		static public function selectField(field:DisplayObject, value:Object, campo:String = 'codigo') : void 
		{
			
			if (SMUtils.isNullOrEmpty(value)) 
			{
				return;
			}
			
			if (field is TextInput) 
			{
				TextInput(field).text = String(value);	
			} 
			else if (field is TextArea) 
			{
				var jcTextArea:TextArea = field as TextArea;
				
				TextArea(field).text = String(value);
				
			} 
			else if (field is RichTextEditor) 
			{
				
				var jcRichTextEditor:RichTextEditor = field as RichTextEditor;
				
			} 
			else if (field is Text) 
			{
				
				var jcText:Text = field as Text;
				
			}
			else if (field is Label) 
			{
				
				var jcLabel:Label = field as Label;
				
			}
			else if (field is DateField) 
			{
				if (value is Date) 
				{
					DateField(field).selectedDate = value as Date;
				} 
				else
				{
					DateField(field).text = String(value);	
				}				
			} 
			else if (field is ComboBox) 
			{
				ComboBox(field).selectedItem = value;
			} 
			else if (field is NumericStepper) 
			{
				NumericStepper(field).value = Number(value);
			}
			else if (field is Image) 
			{
				Image(field).source = value;
			} 
			else if (field is List) 
			{
				SMUtils.selectList(field as List, value, campo);
			} 
			else if (field is DataGrid) 
			{
				DataGrid(field).dataProvider = value; 
			}
			else if (field is ColorPicker) 
			{
				ColorPicker(field).selectedColor = uint(value);
			}
			else if (field is CheckBox) 
			{
				if (value is Boolean)
				{
					CheckBox(field).selected = Boolean(value);
				} 
				else
				{
					
					var newValue:String = SMStringUtils.trim(value.toString());
					
					if (newValue == 'S' || newValue == '1' || newValue == 'true') 
					{
						CheckBox(field).selected = true;
					} 
					else if (newValue == 'N' || newValue == '0' || newValue == 'false') 
					{
						CheckBox(field).selected = false;
					}
					
				}
				
			}
			else if (field is RadioButton) 
			{
				
				if (SMStringUtils.trim(value.toString()) == SMStringUtils.trim(RadioButton(field).value.toString())) 
				{
					RadioButton(field).selected = true;
				} 
				else
				{
					RadioButton(field).selected = true;
				}
				
			}
			
		}
		
		static public function isComponentRequired(component:UIComponent) : Boolean
		{
			
			if (component.hasOwnProperty('required') && Object(component).required)
			{
				return true;
			}
			
			return false;
			
		}
		
	}
}